package com.core.runtimepermission;

import android.app.Activity;

import com.tbruyelle.rxpermissions.RxPermissions;

import rx.Subscriber;

/**
 * @author 李澄锋<br>
 * 2018/8/29
 */
public class NicePermission {

    private Activity activity;
    private NicePermissionListener permissionListener = null;

    public NicePermission(Activity activity, NicePermissionListener permissionListener) {
        this.activity = activity;
        this.permissionListener = permissionListener;
    }

    public void requestPermission(final String... permissons) {
        requestSet(permissons);

    }
//        private void requestOne(final String permisson) {
//            RxPermissions rxPermissions = new RxPermissions(AbstractActivity.this);
//            rxPermissions
//                    .request(permisson).subscribe(new Action1<Boolean>() {
//                @Override
//                public void call(Boolean aBoolean) {
//                    onGranted(aBoolean);
//                    ToastUtil.showToastMessage(AbstractActivity.this, permisson.toString() + ", aBoolean = " + aBoolean);
//
//                }
//            });
//        }

    private void requestSet(String[] permissons) {

        RxPermissions rxPermissions = new RxPermissions(activity);
        rxPermissions.request(permissons).subscribe(new Subscriber<Boolean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Boolean granted) {
                if (granted) {  // All requested permissions are granted
                    if (permissionListener != null) {
                        permissionListener.onGranted();
                    }
                } else {  // At least one permission is denied
                    if (permissionListener != null) {
                        permissionListener.onRefused();
                    }
                }
//                    NiceLogUtil.i("RxPermissions" + " , permission.granted = " + permission.granted + " , permission.shouldShowRequestPermissionRationale = "
//                            + permission.shouldShowRequestPermissionRationale + " , permission.name = " + permission.name + " , permission.toString() = " + permission.toString());
            }
        });
    }
}
